package org.icosagon.ui.builder;

import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.icosagon.utils.Time;

/**
 *
 * @author Daniel Owens
 */
public class TimeField extends JComponent {
    private Time time = new Time();

    private JLabel label = new JLabel();
    private JTextField value = new JTextField();
    private JComboBox unit = new JComboBox();

    public TimeField(String label) {
	this.setLayout(new FlowLayout());

	this.value.getDocument().addDocumentListener(new ValueChangedListener());

	this.unit.addItem("Second");
	this.unit.addItem("Round");
	this.unit.addItem("Hour");
	this.unit.addItem("Day");
	this.unit.addItemListener(new UnitSelectionListener());

	this.label.setText(label);
	this.value.setText(this.time.getValue().getEquation());
	this.unit.setSelectedItem(time.getUnit());

	this.add(this.label);
	this.add(this.value);
	this.add(this.unit);
    }

    public void setTime(Time value) {
	this.time = value;
    }

    public Time getTime() {
	return this.time;
    }

    public void setLabel(String value) {
	this.label.setText(value);
    }

    public String getLabel() {
	return this.label.getText();
    }

    private class UnitSelectionListener implements ItemListener {
	public void itemStateChanged(ItemEvent e) {
	    if(e.getStateChange() == ItemEvent.SELECTED) {
		time.setUnit(e.getItem().toString());
	    }
	}
    }

    private class ValueChangedListener implements DocumentListener {

	public void insertUpdate(DocumentEvent e) {
	    time.getValue().setEquation(value.getText());
	}

	public void removeUpdate(DocumentEvent e) {
	    time.getValue().setEquation(value.getText());
	}

	public void changedUpdate(DocumentEvent e) {
	    throw new UnsupportedOperationException("Not supported yet.");
	}
	
    }
}